﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxTreeList;
using System.Data.SqlClient;

public partial class BaseManage_DistributionCompetences : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        DDataBind();
    }

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (e.Parameter.Equals("null"))
        {
            throw new Exception("请选择人员！");
        }

        string right = "";
        if (treelist.GetSelectedNodes().Count > 0)
        {
            List<TreeListNode> selection = treelist.GetSelectedNodes();

            foreach (TreeListNode node in selection)
            {
                right += node.GetValue("FucnctionID") + "|";
            }
        }
        List<SqlParameter> Params = new List<SqlParameter>();
        Params.Add(new SqlParameter("@right", right));
        Params.Add(new SqlParameter("@userId", e.Parameter));
        Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("update BI_User set Rights=@right where UserID=@userId", Params.ToArray());
    }

    protected void treelist_CustomCallback(object sender, TreeListCustomCallbackEventArgs e)
    {
        string[] Rights;
        //选择人员后刷新当前人员的权限组信息
        string right = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select Rights from BI_User where UserID =" + e.Argument).ToString();

        Rights = right.Split('|');
        //清楚所有选择
        for (int i = 0; i < treelist.Nodes.Count; i++)
        {
            treelist.Nodes[i].Selected = false;
        }

        //确定是否有权限
        if (Rights.Length > 0)
        {
            for (int i = 0; i < Rights.Length; i++)
            {
                if (!Rights[i].Equals(""))
                {
                    TreeListNode SelectedNode = treelist.FindNodeByKeyValue(Rights[i]);

                    if (SelectedNode != null)
                    {
                        SelectedNode.Selected = true;
                    }
                }
            }
        }
    }

    void DDataBind()
    {
        DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from dbo.BI_FFunctions");
        treelist.DataSource = table;
        treelist.DataBind();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }

}